/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jiadakeji.ppm.modules.business.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jiadakeji.ppm.common.config.Global;
import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.modules.sys.utils.UserUtils;
import com.jiadakeji.ppm.modules.business.entity.Community;
import com.jiadakeji.ppm.modules.business.service.CommunityService;

/**
 * 小区Controller
 * @author HHAOYU
 * @version 2019-07-08
 */
@Controller
@RequestMapping(value = "${adminPath}/business/community")
public class CommunityController extends BaseController {

	@Autowired
	private CommunityService communityService;
	
	@ModelAttribute
	public Community get(@RequestParam(required=false) String id) {
		Community entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = communityService.get(id);
		}
		if (entity == null){
			entity = new Community();
		}
		return entity;
	}
	
	@RequiresPermissions("business:community:view")
	@RequestMapping(value = {"list", ""})
	public String list(Community community, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Community> page = communityService.findPage(new Page<Community>(request, response), community); 
		model.addAttribute("page", page);
		
		UserUtils.putCache("community", community);
		return "modules/business/communityList";
	}

	@RequiresPermissions("business:community:view")
	@RequestMapping(value = "form")
	public String form(Community community, Model model) {
		model.addAttribute("community", community);
		return "modules/business/communityForm";
	}

	@RequiresPermissions("business:community:edit")
	@RequestMapping(value = "save")
	public String save(Community community, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, community)){
			return form(community, model);
		}
		communityService.save(community);
		addMessage(redirectAttributes, "保存小区成功");
		
		redirectAttributes.addFlashAttribute("community", UserUtils.getCache("community"));
		return "redirect:"+Global.getAdminPath()+"/business/community/?repage";
	}
	
	@RequiresPermissions("business:community:edit")
	@RequestMapping(value = "delete")
	public String delete(Community community, RedirectAttributes redirectAttributes) {
		communityService.delete(community);
		addMessage(redirectAttributes, "删除小区成功");
		
		redirectAttributes.addFlashAttribute("community", UserUtils.getCache("community"));
		return "redirect:"+Global.getAdminPath()+"/business/community/?repage";
	}

}